<?php
/**
 * Created by PhpStorm.
 * User: dong
 * Date: 2018/7/10
 * Time: 0:00
 */
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\BankTraits\CYS8BankMap;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class CYS8Payment extends RechargeBaseAbstract implements WAPable
{
    use CYS8BankMap;

    protected $gw_pay = 'http://api.cys8.com/pay/ap.php';
    protected $gw_query = 'http://api.cys8.com/pay/payresult.php';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['result_code'] == 0) return true;
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $signStr = "";
        $params = $data;
        unset($params['sign']);

        foreach ($params as $key => $param) {
            $signStr .= "{$key}={$param}&";
        }
        $signStr .= "key=" . $this->getMchKey();
        $sign = strtoupper(md5($signStr));
        if (strcasecmp($sign, $data['sign']) == 0) return true;
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        return $data['cp_trade_no'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return bcdiv($data['fee'], 100, 2);
    }

    public function getThirdNoFromCallback(array $data)
    {
        return $data['pay_transaction_id'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::CYS8;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::WX => '11',
            PaymentMap::WX_WAP => '12',
            PaymentMap::ALI => '21',
            PaymentMap::ALI_WAP => '22',
            PaymentMap::QQ => '31',
            PaymentMap::QQ_WAP => '32',
            PaymentMap::JD => '41',
            PaymentMap::JD_WAP => '42',
            PaymentMap::BANK => '51',
            PaymentMap::BANK_WAP => '52'
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('cp_trade_no', $rechargeOrder->plat_no);
        $this->setParameter('cpid', $this->getMchId());
        $this->setParameter('pay_day', date('Y-m-d'));
        $this->setParameter('sign', $this->paySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters());
        if ($res['http_code'] == 200) {
            $body = json_decode($res['body'], true);
            if (array_key_exists('result_code', $body) && $body['result_code'] == 0) return true;
            return $body['result_code'];
        }
        return $res['body'];
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() .'?'. http_build_query($this->getParameters());
//        $res = curlHttp($this->getGwPay(), $this->getParameters(), 'get');
//        if ($res['http_code'] == 200) {
//            $body = json_decode($res['body'], true);
//            if ($body['result_code']) {
//
//            }
//        }
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $params = $this->getParameters();
        ksort($params);
        $signStr = "";
        foreach ($params as $key => $param) {
            $signStr .= "{$key}={$param}&";
        }
        $signStr .= "key=". $this->getMchKey();
        return strtoupper(md5($signStr));
    }


    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return '';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
        $mchData = $rechargeOrder->order_data;
        $this->setParameter('spid', '8');
        $this->setParameter('cpid', $this->getMchId());
        $this->setParameter('cp_trade_no', $rechargeOrder->plat_no);
        $this->setParameter('fee', bcmul($rechargeOrder->order_amt, 100));
        $this->setParameter('jump_url', $this->getReturnUrl());
        $this->setParameter('notify_url', $this->getCallbackUrl());
        $this->setParameter('pay_type', $this->getPaymentMap($mchData['recharge_type']));
        if (in_array('bank_code', $mchData)) {
            $this->setParameter('bank_code', $this->getBank($mchData['bank_code']));
        }
        $this->setParameter('sign', $this->paySign());
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        return new HrefRedirectAction($this->getGwPay() .'?'. http_build_query($this->getParameters()));
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
    }
}